import CryptoJS from 'crypto-js'

class Crypoto {
  constructor() {
    this.key = 'abcdefghijkl_key' // 十六位十六进制数作为密钥
    this.iv = 'abcdefghijklm_iv' // 十六位十六进制数作为密钥偏移量
  }

  /** CBC解密 */
  Decrypt(word) {
    const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
    const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
    const _key = CryptoJS.enc.Utf8.parse(this.key)
    const _iv = CryptoJS.enc.Utf8.parse(this.iv)
    const decrypt = CryptoJS.AES.decrypt(srcs, _key, { iv: _iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
  }

  /** CBC加密 */
  Encrypt(word) {
    const srcs = CryptoJS.enc.Utf8.parse(word)
    const _key = CryptoJS.enc.Utf8.parse(this.key)
    const _iv = CryptoJS.enc.Utf8.parse(this.iv)
    const encrypted = CryptoJS.AES.encrypt(srcs, _key, { iv: _iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
    return encrypted.ciphertext.toString().toUpperCase()
  }
}

export default Crypoto
